package com.until;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component //组件
@EnableScheduling  //开启定时任务
public class AlarmTask {
    private static final SimpleDateFormat myFormat = new SimpleDateFormat("HH:mm:ss");
    private static int onlineCounter = 0;

    @Scheduled(cron = "0/5 * * * * *")
    public void run() throws InterruptedException {
        Thread.sleep(6000);
        System.out.println(onlineCounter++);
        System.out.println("cron 当前时间" + myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName() + "====->" + System.currentTimeMillis()/1000);
    }

    @Scheduled(fixedRate = 5000)
    public  void run1() throws InterruptedException {
        Thread.sleep(6000);
        System.out.println("fixedRate 当前时间" + myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName() + "====->" + System.currentTimeMillis()/1000);
    }

    @Scheduled(fixedDelay = 5000)
    public void run2() throws InterruptedException {
        Thread.sleep(6000);
        System.out.println("fixedDelay 当前时间" + myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName() + "====->" + System.currentTimeMillis()/1000);
    }

    @Scheduled(initialDelay = 2000, fixedDelay = 5000)
    public void run3() throws InterruptedException {
        System.out.println("initialDelay 当前时间" + myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName() + "====->" + System.currentTimeMillis()/1000);
        System.out.println("fixedDelay 当前时间" + myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName() + "====->" + System.currentTimeMillis()/1000);
    }
}
